package com.pinyoumei.app.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.GiftProcessBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.StringColorUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.utils.Utils;
import com.xw.repo.BubbleSeekBar;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2020-07-14.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-07-14        Administrator             新增Person类
 * 2020-07-14        Administrator             增加sex属性
 * 团队进阶
 */
public class TeamAdvancedActivity extends BaseActivity implements View.OnClickListener {


    private int color;
    //    private TextView tv_speak1;
    private TextView tv_speak2;
    private BubbleSeekBar seek_bar;
    private CircleImageView headImage;
    private TextView tv_name;
    private ImageView tv_lv_type;
    private TextView tv_user_num;
    private TextView tv_all_num;
    private TextView tv_service_num;
    private TextView tv_inrect_num;
    private TextView tv_team_num;

    private TextView tv_team_server_num;
//    private TextView tv_run_center_num;
//    private TextView tv_team_center_num;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_advanced);

        initWidget();

        InitData();
    }


    private void initWidget() {

        NestedScrollView scroll = findViewById(R.id.scroll);
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);

        headImage = findViewById(R.id.image_user_icon);
        tv_name = findViewById(R.id.tv_name);
        tv_lv_type = findViewById(R.id.tv_lv_type);
        tv_user_num = findViewById(R.id.tv_user_num);
        tv_all_num = findViewById(R.id.tv_all_num);
        tv_service_num = findViewById(R.id.tv_service_num);
        tv_inrect_num = findViewById(R.id.tv_inrect_num);
        tv_team_num = findViewById(R.id.tv_team_num);
        tv_team_server_num = findViewById(R.id.tv_team_server_num);
//        tv_run_center_num = findViewById(R.id.tv_run_center_num);
//        tv_team_center_num = findViewById(R.id.tv_team_center_num);


        color = ContextCompat.getColor(this, R.color.orange_text_color);

//        tv_speak1 = findViewById(R.id.tv_speak_1);
        tv_speak2 = findViewById(R.id.tv_speak_2);


        seek_bar = findViewById(R.id.seek_bar);
        scroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView nestedScrollView, int i, int i1, int i2, int i3) {
                seek_bar.correctOffsetWhenContainerOnScrolling();

            }
        });


    }


    private void InitData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(this));

        OkGo.<String>post(Constants.GIFT_PROCESS)
                .params("user_id", SPUtils.getAppUserId(this))
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        GiftProcessBean giftProcessBean = gson.fromJson(response.body(), GiftProcessBean.class);
                        if (giftProcessBean.getCode() >= 0) {
                            Glide.with(TeamAdvancedActivity.this).load(giftProcessBean.getResult().getUser_head_img()).into(headImage);

                            tv_name.setText(giftProcessBean.getResult().getUser_real_name());
//                            if(TextUtils.equals(giftProcessBean.getResult().getUser_level_name(),"V4") || TextUtils.equals(giftProcessBean.getResult().getUser_level_name(),"V5")){
//                                tv_lv_type.setText("V3");
//                            }else {
//                                tv_lv_type.setText(giftProcessBean.getResult().getUser_level_name());
//                            }
                            tv_lv_type.setImageDrawable(getResources().getDrawable(Utils.getLevelIcon(giftProcessBean.getResult().getUser_level_name())));
//                            seek_bar.getConfigBuilder().sectionCount(giftProcessBean.getResult().getLevel_list().size() - 1).build();
                            seek_bar.getConfigBuilder().sectionCount(4).build();
//                            1普通用户 2至尊会员 3创业合伙人 4门店

                            if (giftProcessBean.getResult().getLevel_list() != null) {
                                seek_bar.setCustomSectionTextArray(new BubbleSeekBar.CustomSectionTextArray() {
                                    @NonNull
                                    @Override
                                    public SparseArray<String> onCustomize(int sectionCount, @NonNull SparseArray<String> array) {
                                        array.clear();

                                        for (int i = 0; i < giftProcessBean.getResult().getLevel_list().size(); i++) {
                                            array.append(i, giftProcessBean.getResult().getLevel_list().get(i).getLevel_name());
                                            if (giftProcessBean.getResult().getUser_level_name().equals(giftProcessBean.getResult().getLevel_list().get(i).getLevel_name())) {

                                                if (giftProcessBean.getResult().getLevel_list().size() == 4) {
                                                    switch (i) {
                                                        case 0:
                                                            seek_bar.setProgress(0);
                                                            break;


                                                        case 1:
                                                            seek_bar.setProgress(33);
                                                            break;


                                                        case 2:
                                                            seek_bar.setProgress(66);
                                                            break;

                                                        case 3:
                                                            seek_bar.setProgress(100);
                                                            break;

                                                        default:
                                                            break;
                                                    }
                                                }

                                                if (giftProcessBean.getResult().getLevel_list().size() == 5) {
                                                    switch (i) {
                                                        case 0:
                                                            seek_bar.setProgress(0);
                                                            break;


                                                        case 1:
                                                            seek_bar.setProgress(25);
                                                            break;


                                                        case 2:
                                                            seek_bar.setProgress(50);
                                                            break;

                                                        case 3:
                                                            seek_bar.setProgress(75);
                                                            break;
                                                        case 4:
                                                            seek_bar.setProgress(100);
                                                            break;

                                                        default:
                                                            break;
                                                    }
                                                }
                                            }

                                        }
                                        sectionCount = array.size();
                                        return array;
                                    }
                                });


                            }


                            //团队人数 total_team_num
                            tv_user_num.setText(giftProcessBean.getResult().getTotal_team_num() + "");


                            //累计至尊会员 total_spell_user_num
                            tv_all_num.setText(giftProcessBean.getResult().getTotal_spell_user_num() + "");

                            //直推至尊会员 direct_spell_user_num
                            tv_inrect_num.setText(giftProcessBean.getResult().getDirect_spell_user_num() + "");

                            //团队至尊会员 direct_team_leader_num
                            tv_team_num.setText(giftProcessBean.getResult().getTeam_leader_num() + "");


                            //直推创业合伙人 team_leader_num
                            tv_service_num.setText(giftProcessBean.getResult().getTeam_leader_num() + "");

                            //团队创业合伙人 direct_partner_num
                            tv_team_server_num.setText(giftProcessBean.getResult().getDirect_partner_num() + "");

//                            //直推门店（人）
//                            tv_run_center_num.setText(giftProcessBean.getResult().getCenter_num()+"");
//
//                            // 团队门店（人）
//                            tv_team_center_num.setText(giftProcessBean.getResult().getTeam_center_num()+"");


//                            tv_speak1.setText(StringColorUtils.setNumColor(giftProcessBean.getResult().getRule_desc()+"",color));

                            tv_speak2.setText(StringColorUtils.setNumColor(giftProcessBean.getResult().getProcess_desc() + "", color));

                        } else {
                            ToastUtils.showShort(giftProcessBean.getMsg());
                        }

                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.relative_finish:
                finish();
                break;

            default:
                break;
        }
    }


}
